package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.Scroller;

import java.lang.reflect.Field;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-09
 * Time: 09:44
 * Description: 公共组件库：滑动缓慢的ViewPager
 */
public class SlowViewPager extends ViewPager {
    private int mDuration = 1000;

    public void setDuration (int duration) {
        mDuration = duration;
    }

    public SlowViewPager (Context context) {
        super (context);
        init ();
    }


    public SlowViewPager (Context context, AttributeSet attrs) {
        super (context, attrs);
        init ();
    }

    private void init () {
        try {
            Field mScroller = ViewPager.class.getDeclaredField ("mScroller");
            mScroller.setAccessible (true);
            FixedSpeedScroller scroller = new FixedSpeedScroller (getContext ());
            mScroller.set (this, scroller);
        } catch (Exception ignored) {
        }
    }

    public class FixedSpeedScroller extends Scroller {

        FixedSpeedScroller (Context context) {
            super (context);
        }

        @Override
        public void startScroll (int startX, int startY, int dx, int dy, int duration) {
            super.startScroll (startX, startY, dx, dy, mDuration);
        }

        @Override
        public void startScroll (int startX, int startY, int dx, int dy) {
            super.startScroll (startX, startY, dx, dy, mDuration);
        }
    }
}